var app = getApp();
const FN = require('../../utils/public');
import commonAPI from '../../api/api'
var page = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingEnd: "none",
    vsObj: [],
    booksId: '',
    userType:"03",
    curIndex: -1,
    isShowCatalogList: false
  },

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      booksId: options.booksId==null?options.scene:options.booksId,
      userType:app.globalData.userInfo.userType
    })
    that.initVSArrData();
  },

    // 所有
    initVSArrData: function () {
      var that = this;
      const data = {
        booksId: that.data.booksId,
        pageNum: 1,
        pageSize: 15
      };
      commonAPI.getBookAndVSinfoByopenId({ ...data }).then(res => {
        if (res.code == '200') {
          var bookVoiceList = 'vsObj.bookVoiceList';
          var bcInfo = 'vsObj.bcInfo';
          that.setData({
            [bcInfo]: res.data,
            [bookVoiceList]: res.data.bookVoiceList,
            curIndex: -1
          })
        }
      })
  
    },

  // 根据章节id获取语音 
  getChapteInfo: function (e) {
    var that = this,
      bookChapteId = e.currentTarget.dataset.chapteid,
      index = e.currentTarget.dataset.curindex;
      const data = {
        booksId: that.data.booksId,
        bookChapteId: bookChapteId
      };
      commonAPI.getVSListBychapteId({ ...data }).then(res => {
        if (res.code == '200') {
          var bookVoiceList = 'vsObj.bookVoiceList';
      that.setData({
        [bookVoiceList]: res.data.bookVoiceList,
        isShowCatalogList: false,
        curIndex: index
      })
        }
      })
  },

  // 设置选中
  setChecked(e){
    const id = e.currentTarget.dataset.id
    const arr = this.data.vsObj.bcInfo.bookChapteList;
    arr.map((ele) =>{
      if(ele.bookChapteId == id){
        ele.hasSelected = !ele.hasSelected;
      }
    })
    var bookVoiceList = 'vsObj.bcInfo.bookChapteList';
    this.setData({
      [bookVoiceList] : arr
    })
  },

  // 展开目录
  catalogList: function () {
    var that = this;
    that.setData({
      isShowCatalogList: true
    })
  },

  closeCatalogList: function () {
    var that = this;
    that.setData({
      isShowCatalogList: false
    })
  },

  // 跳转预设发音技巧页面
  goPresetsPage(){
    let arr = this.data.vsObj.bcInfo.bookChapteList;
    let ids = [];
    arr.forEach(item =>{
      if(item.hasSelected){
        ids.push(item.bookChapteId)
      }
    })
    if(ids.length == 0){
      FN.Toast("请选择章节");
        return false;
    }
    wx.navigateTo({
      url: '../admin4TeacherPage/presetsPronunciationSkills/presetsPronunciationSkills?bcIds='+ids.join(","),
    })
  },

  // 上拉继续加载更多数据
  initVSArrData4pushUp: function (pageNum) {
    var that = this;
    const data = {
      booksId: that.data.booksId,
      pageNum: pageNum,
      pageSize: 15
    };
    commonAPI.getBookAndVSinfoByopenId({ ...data }).then(res => {
      if (res.code == '200') {
        if (res.data.bookVoiceList.length == 0) {
          FN.Toast("没有更多数据了")
        } else {
          var resArr = that.data.vsObj.bookVoiceList,
            bookVoiceList = "vsObj.bookVoiceList";
          resArr.push.apply(resArr, res.data.bookVoiceList); //上拉加载
          that.setData({
            [bookVoiceList]: resArr
          })
        }
      }
    })
  },
  
  // 历史作业跟读
  voiceRepeat: function (e) {
    var that = this;
    wx.navigateTo({
      url: "../card/card?bookChapteId=" +that.data.vsObj.bookVoiceList[e.currentTarget.dataset.voiceindex].bookChapteId+"&voiceSystemId="+that.data.vsObj.bookVoiceList[e.currentTarget.dataset.voiceindex].voiceSystemId
    })
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.curIndex == -1) {
      page++;
      that.initVSArrData4pushUp(page);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    var t = JSON.stringify(that.data.scoreDetail);
    let title, imageUrl;
    if (res.from === 'menu') {
      title = '跟着口语主题，提高你的发音水平';
    }
    return {
      title: title,
      imageUrl: imageUrl, //这个是分享的图片
      path: 'pages/myBooksChapter/myBooksChapter?booksId=' + that.data.booksId,
    }
  }
})